package sparkCore

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo01_wordCount {

  def wordCount(sc:SparkContext,filePath:String) = {
    val dataRDD:RDD[String] = sc.textFile(filePath)
    val wordRDD:RDD[String] = dataRDD.flatMap(_.split(" "))
    val kvRDD:RDD[(String,Int)] = wordRDD.map((_,1))
    val resultRDD:RDD[(String,Int)] = kvRDD.reduceByKey(_+_)
    val result:Unit = resultRDD.foreach(println)
  }

  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("wc")
    val sc: SparkContext = new SparkContext(conf)
    wordCount(sc,args(0))
//    Thread.sleep(1000000000)

    sc.stop()
  }

}
